﻿using Customer.API.Applcation.Command.VendorCommand;
using Customer.API.Dtos;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace Customer.API.Controllers
{
    /// <summary>
    /// 供应商管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    
    public class VendorController : ControllerBase
    {
        private readonly IMediator mediator;
        public VendorController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 供应商添加
        /// </summary>
        /// <param name="command">供应商实体</param>
        /// <returns>受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddVendor(AddVendorCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.User_Id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                result.Data = await mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改供应商
        /// </summary>
        /// <param name="command">要修改的实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdVendor(UpdVendorCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.User_Id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                result.Data = await mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除供应商
        /// </summary>
        /// <param name="command">要删除的实体id</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelVendor(DelVendorCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
    }
}
